﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Security;
using System.IO;
using System.Text.RegularExpressions;


namespace SHack.Helper
{

     public class HttpHelperLite
    {

        public int TimeOut = 10000;
        private static Regex reg=new Regex(@"\(\d\)");

        public int Get(string url)
        {
            try
            {
                HttpWebRequest request;
                if (url.ToLower().StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.ProtocolVersion = HttpVersion.Version11;
                }
                else
                    request = (HttpWebRequest)HttpWebRequest.Create(url);
                //request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
                //request.Accept = "application/json, text/javascript, */*; q=0.01";
                request.Timeout = TimeOut;
                request.AllowAutoRedirect = false;
                
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                request.Abort();
                //Stream sr=response.GetResponseStream();

                //byte[] data=new byte[1];
                //sr.Read(data, 0, data.Length);
                //sr.Dispose();

                return (int)response.StatusCode;
            }
            catch (Exception ex){
                //Match mc = reg.Match(ex.Message);
                return 404;
            }
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受  
        }  
    }

}
